/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 */
'use strict';

var React = require('react-native');
var {
  AppRegistry,
  Image,
  ListView,
  StyleSheet,
  Text,
  View,
} = React;

var REQUEST_URL = 'https://raw.githubusercontent.com/facebook/react-native/master/docs/MoviesExample.json';

var WalkerfreeProject = React.createClass({
  getInitialState:function(){
    return {
      dataSource:new ListView.DataSource({
        rowHasChanged:(row1, row2) => row1 != row2
      }),
      loaded:false
    }
  },
  componentDidMount:function(){
    this.fetchData();
  },
  fetchData:function(){
    fetch(REQUEST_URL)
      .then((response) => response.json())
      .then((responseData) => {
        this.setState({
          dataSource:this.state.dataSource.cloneWithRows(responseData.movies),
          loaded:true
        });
      })
      .done()
  },
  renderLoadingView:function(){
    return (
      <View style={styles.container}>
        <Text>
          Loading Views
        </Text>
      </View>
    );
  },
  renderMovie:function(movie){
    return (
      <View style={styles.container} key={movie.id}>
        <Image source={{uri:movie.posters.thumbnail}} style={styles.thumbnail} />
        <View style={styles.rightContainer}>
          <Text style={styles.title}>{movie.title}</Text>
          <Text style={styles.year}>{movie.year}</Text>
        </View>
      </View>
    );
  },
  render: function() {
    if(!this.state.loaded){
      return this.renderLoadingView();
    }

    return (
      <ListView dataSource={this.state.dataSource}
                renderRow={this.renderMovie}
                style={styles.listView} />
    );
  }
});

var styles = StyleSheet.create({
  lisView:{
    paddingTop:20,
    backgroundColor:'red'
  },
  container: {
    flex: 1,
    flexDirection:'row',
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#F5FCFF',
  },
  thumbnail: {
    width: 53,
    height: 81,
  },
  rightContainer:{
    flex:1
  },
  title:{
    fontSize:20,
    marginBottom:8,
    textAlign:'center'
  },
  year:{
  	textAlign:'center'
  }
});

AppRegistry.registerComponent('WalkerfreeProject', () => WalkerfreeProject);
